package nina.yiyou.performance.s11collect.output

import nina.yiyou.performance.s11collect.compute.EvalCommentCollector
import nina.yiyou.salary.input.util.InputPathResolver

import java.nio.file.Files
import java.nio.file.Path
import java.nio.file.Paths
import java.util.stream.Collectors

class CollectResultGenerator {

  CollectResultGenerator(Path workshopPath, Map configMap, List<Map> employeeResult, List<EvalCommentCollector.Comment> commentResult) {
    _workshopPath = workshopPath
    _configMap = configMap
    _employeeResult = employeeResult
    _commentResult = commentResult
  }

  void generate() {
    //TODO: 改成到模板excel读
    List employeeKeys = ['已填.工号', '已填.姓名'] + makeKeyList([
        ['自评', '环评', '直属上级评', '部门主管评'],
        ['工作业绩', '工作能力', '工作态度'],
    ]) + ['已填.自评']

    // prepareExport
    Path outDir = InputPathResolver.create(_workshopPath, _configMap, 'out.绩效汇总').resolve()
    Files.createDirectories(outDir)

    Path tmpDir = Paths.get(outDir.toString() + '_tmp')
    tmpDir.deleteDir()
    Files.createDirectories(tmpDir)

    new CollectEmployeeCsvExporter(_employeeResult, employeeKeys, tmpDir.resolve('分数汇总.csv')).export()
    new OtherCommentExporter(_commentResult, tmpDir.resolve('他评评价汇总.csv')).export()
  }

  private List<String> makeKeyList(List<List> key) {
    List elem = key[1]
    return key[0].stream()
        .flatMap { String t -> elem.stream().map { "计算.${t}_${it}" } }
        .collect(Collectors.toList())
  }

  private final Path _workshopPath
  private final Map _configMap

  private final List<Map> _employeeResult
  private final List<EvalCommentCollector.Comment> _commentResult
}
